package com.example.exam.sysRoleResourcePermission.dao;

import com.alibaba.fastjson.JSON;
import com.example.exam.db.BaseDao;
import com.example.exam.sysRoleResourcePermission.entity.SysRoleResourcePermissionBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class SysRoleResourcePermissionDao extends BaseDao<SysRoleResourcePermissionBean, Integer> {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String findByIdSql() {
		return "select * from sys_role_resource_permission where id = ?";
	}
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getAddSql() {
		return "insert into sys_role_resource_permission (id, role_id, resource_id, permission_id, state, create_time, create_user) values (:id, :roleId, :resourceId, :permissionId, :state, :createTime, :createUser) ";
	}
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getUpdateAllFieldSql() {
		return "update sys_role_resource_permission set role_id=:roleId, resource_id=:resourceId, permission_id=:permissionId, state=:state, create_time=:createTime, create_user=:createUser where id=:id ";
	}
	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getRemoveByIdSql() {
		return "update sys_role_resource_permission set state = -1 where id = ? ";
	}

	@Override
	    /**
	        * @Description:
	        * @Param: []
	        * @return: java.lang.String
	        * @Author: xhw
	        * @Date: 2021/7/26
	        */
	public String getDeleteByIdSql() {
		return "delete from sys_role_resource_permission where id = ? ";
	}
	/**
	 * @Description:
	 * @Param: [roleId, resourceId, permissionId]
	 * @return: com.example.exam.sysRoleResourcePermission.entity.SysRoleResourcePermissionBean
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */public SysRoleResourcePermissionBean queryDataByRrp(String roleId, Integer resourceId, Integer permissionId) {
	 	try {
			List<Object> list = new ArrayList<Object>();
			String sql = "SELECT * FROM sys_role_resource_permission WHERE state = 1";
			if (!StringUtils.isEmpty(roleId)) {
				sql += " AND role_id = ?";
				list.add(roleId);
			}
			if (resourceId != null) {
				sql += " AND resource_id = ?";
				list.add(resourceId);
			}
			if (permissionId != null) {
				sql += " AND permission_id = ? ";
				list.add(permissionId);
			}
			return getJtl().queryForObject(sql, list.toArray(),
					new BeanPropertyRowMapper<SysRoleResourcePermissionBean>(SysRoleResourcePermissionBean.class));
		} catch (Exception e) {
			e.getStackTrace();
			return null;
		}

	}/**
	 * @Description:
	 * @Param: [roleId, resourceId, permissionId]
	 * @return: int
	 * @Author: xhw
	 * @Date: 2021/7/26
	 */
	public int delDataByRoleId(Integer roleId,Integer resourceId,Integer permissionId) {
		String sql = "DELETE FROM sys_role_resource_permission WHERE role_id =? AND resource_id =? AND permission_id =?";
		List<Object> list=new ArrayList<>();
		list.add(roleId);
		list.add(resourceId);
		list.add(permissionId);
		Integer i=getJtl().update(sql,list.toArray());
		return i;
	}
	public SysRoleResourcePermissionBean queryBean(Integer userId, Integer resourceId, Integer permissionId) {
		try {
			String sql = "SELECT * FROM sys_role_resource_permission t where t.state=1 and t.resource_id=? and t.permission_id=? and t.role_id in "
					+ " (select sr.role_id from sys_user_role sr where sr.state=1 and sr.user_id=?) limit 1";
			return getJtl().queryForObject(sql, new Object[] { resourceId, permissionId, userId },
					new BeanPropertyRowMapper<SysRoleResourcePermissionBean>(SysRoleResourcePermissionBean.class));
		} catch (DataAccessException e) {
			return null;
		}
	}
}